//
//  TxLivePushView.m
//  tx_live_plugin
//
//  Created by FXY on 2022/6/26.
//

#import "TxLivePushView.h"

#import <TXLiteAVSDK_Professional/TXLivePush.h>

@interface TxLivePushView ()<TXLivePushListener>

@property (nonatomic, strong) TXLivePush * txLivePublisher;
@property (nonatomic, strong) TXLivePushConfig * txLivePushConfig;

/// 闪光灯开启
@property (assign, nonatomic) BOOL flashed;

@end

@implementation TxLivePushView

- (TXLivePushConfig *)txLivePushConfig{
    if (!_txLivePushConfig) {
        _txLivePushConfig = [[TXLivePushConfig alloc] init];
        _txLivePushConfig.frontCamera = NO;
        _txLivePushConfig.connectRetryInterval = 3; //推流器连接重试间隔
        _txLivePushConfig.pauseFps = 10;
        _txLivePushConfig.pauseTime = 300;
        _txLivePushConfig.enableHWAcceleration = false;
        _txLivePushConfig.connectRetryCount = 5; //推流重联次数
    }
    return _txLivePushConfig;
}

- (TXLivePush *)txLivePublisher{
    if (!_txLivePublisher) {
        _txLivePublisher = [[TXLivePush alloc] initWithConfig:self.txLivePushConfig];
        [_txLivePublisher setVideoQuality:VIDEO_QUALITY_STANDARD_DEFINITION adjustBitrate:NO adjustResolution:NO];
        _txLivePublisher.delegate = self;
    }
    return _txLivePublisher;
}

- (instancetype)initWithFrame:(CGRect)frame args:(NSDictionary *)args{
    if (self = [super initWithFrame:frame]) {
        [self configData];
    }
    
    return self;
}

/// 基本配置
- (void)configData{
    
    [self.txLivePublisher startPreview:self];
}


#pragma mark - TXLivePushListener
- (void)onPushEvent:(int)evtID withParam:(NSDictionary *)param{
    __weak __typeof__(self) weakSelf = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (evtID == PUSH_EVT_CONNECT_SUCC) //1001
        {
            //链接服务器成功
            
        } else if (evtID == PUSH_EVT_PUSH_BEGIN) { //1002
            // 连接成功 开始推流计算时间
            
        } else if (evtID == PUSH_WARNING_NET_BUSY) {
            
        } else if (evtID == PUSH_ERR_OPEN_CAMERA_FAIL){
            
            [weakSelf stopLivePush];
            
        } else if (evtID == PUSH_ERR_OPEN_MIC_FAIL){
            
            [weakSelf stopLivePush];
        } else if (evtID > abs(PUSH_ERR_OPEN_CAMERA_FAIL) ) {
            //其他的失败
            if(evtID <= abs(PUSH_ERR_AUDIO_SYSTEM_NOT_WORK)){
            
                [weakSelf stopLivePush];
            }
        }
    });
}

- (void)onNetStatus:(NSDictionary *)param{
    
}


- (void)onScreenCaptureStarted{
    
}

- (void)onScreenCapturePaused:(int)reason{
    
}


- (void)onScreenCaptureResumed:(int)reason{
    
}

- (void)onScreenCaptureStoped:(int)reason{
    
}

#pragma mark - flutter事件
- (void)startLivePush:(NSDictionary *)dic{
    if (dic!=nil&&[dic isKindOfClass:NSDictionary.class]) {
        NSString *url = @"";
        NSInteger serverTime = 0;
        BOOL isBitrateStandard = NO;
        if ([dic.allKeys containsObject:@"url"]) {
            url = [dic valueForKey:@"url"];
        }
        if ([dic.allKeys containsObject:@"serverTime"]) {
            serverTime = [[dic valueForKey:@"serverTime"] intValue];
        }
        if ([dic.allKeys containsObject:@"isBitrateStandard"]) {
            isBitrateStandard = [[dic valueForKey:@"isBitrateStandard"] boolValue];
        }
        
        [self.txLivePublisher startPush:url];
    }
}

- (void)stopLivePush{
    [self.txLivePublisher stopPush];
}

- (void)onPause{
    [self.txLivePublisher pausePush];
}

- (void)onResume{
    [self.txLivePublisher resumePush];
}

- (void)onDestroy{
    
}

- (BOOL)isPushRecording{
    return self.txLivePublisher.isPublishing;
}

- (void)turnOnFlashLight{
    self.flashed = !self.flashed;
    [self.txLivePublisher toggleTorch:self.flashed];
}

- (BOOL)isFlashLightOpen{
    return self.flashed;
}

- (BOOL)isFontCamera{
    return self.txLivePublisher.frontCamera;
}

- (void)switchCamera{
    [self.txLivePublisher switchCamera];
}

@end
